// enhanceApp.js
import Router from 'vue-router'
import moment from 'moment'
import Vue from 'vue'

const router = new Router({
  mode: 'history'
})

// 防止相同路由跳转时报错
const VueRouterPush = Router.prototype.push
Router.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}
Vue.prototype.$moment = moment;
export default ({
  Vue,
  router
}) => {
  router.beforeEach((to, from, next) => {
    // 解决非ASCII文件名的路由, 防止 404
    if (typeof _hmt != "undefined") {
      if (to.path) {
        _hmt.push(["_trackPageview", to.fullPath]);
      }
    }
    const decodedPath = decodeURIComponent(to.path)
    if (decodedPath !== to.path) {
      next(Object.assign({}, to, {
        fullPath: decodeURIComponent(to.fullPath),
        path: decodedPath
      }))
      //触发百度的pv统计
    } else {
      next()
    } 
  })
}